package lzk.myshop.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import lzk.myshop.config.MyAlipayConfig;
import lzk.myshop.dao.GoodsMapper;
import lzk.myshop.dao.OrderDetailMapper;
import lzk.myshop.dao.OrderMapper;
import lzk.myshop.pojo.OrderDetail;
import lzk.myshop.service.AlipayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class AlipayServiceImpl implements AlipayService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderDetailMapper orderDetailMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Override
    public void pay(int Orderid,HttpServletResponse response) throws AlipayApiException, IOException {

        int userid = orderMapper.getUseridByOrderID(Orderid);

        AlipayClient alipayClient = new DefaultAlipayClient(new MyAlipayConfig().setConfig());
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();

        model.setOutTradeNo(String.valueOf(Orderid));

        model.setTotalAmount(String.valueOf(orderMapper.getTotalPriceById(Orderid)));
        model.setSubject("商品付款");

        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        request.setReturnUrl("http://127.0.0.1:8081/order?userId"+userid);
        request.setNotifyUrl("https://b7y4284462.zicp.fun/Alipay/callback");
        request.setBizModel(model);


        String from = alipayClient.pageExecute(request).getBody();
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().println(from);
        response.getWriter().flush();
        response.getWriter().close();

    }

    @Override
    public Map<String, Object> callback(HttpServletRequest request) {
        String tradeStatus = request.getParameter("trade_status"); // 交易状态
        String orderNo = request.getParameter("out_trade_no"); // 获取订单号
        String tradeNo = request.getParameter("trade_no"); // 支付宝交易凭证号
        Map<String,Object> result = new HashMap<>();
        if (tradeStatus!=null && tradeStatus.equals("TRADE_SUCCESS")){
            int orderid = Integer.parseInt(orderNo);
            orderMapper.payOrder(orderid);
            List<OrderDetail> orderDetailList = orderDetailMapper.getOrderDByOrderId(orderid);
            for (int i=0;i<orderDetailList.size();i++){
                int goodsid = orderDetailList.get(i).getGoodsid();
                int count = orderDetailList.get(i).getNumber();
                goodsMapper.reduce(count,goodsid);
            }
            result.put("code",1);
            result.put("message","支付成功");
            result.put("data",null);
        }else {
            result.put("code",-1);
            result.put("message","支付失败");
            result.put("data",null);
        }

        return result;
    }
}
